package com.atguigu.tingshu.album.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;

    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;

    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;

    @Autowired
    private BaseCategoryViewMapper baseCategoryViewMapper;

    @Autowired
    private BaseAttributeMapper baseAttributeMapper;


    @Override
    public List<JSONObject> getBaseCategoryList() {

        /**需要获取表中的一级二级三级的数据
         *返回一个list对象
         */
        ArrayList<JSONObject> baseCategoryView1 = new ArrayList<>();
        List<BaseCategoryView> baseCategoryViews1 = baseCategoryViewMapper.selectList(null);
        Map<Long, List<BaseCategoryView>> map = baseCategoryViews1.
                stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));

        //获得一级分类
        for (Map.Entry<Long, List<BaseCategoryView>> entry : map.entrySet()) {
            //返回一个json对象
            JSONObject jsonObject1 = new JSONObject();
            Long key = entry.getKey();
            jsonObject1.put("categoryId", key);
            String category1Name = entry.getValue().get(0).getCategory1Name();
            jsonObject1.put("categoryName", category1Name);

            ArrayList<JSONObject> baseCategoryView2 = new ArrayList<>();
            Map<Long, List<BaseCategoryView>> map1 = entry.getValue()
                    .stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));

            for (Map.Entry<Long, List<BaseCategoryView>> entry1 : map1.entrySet()) {
                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.put("categoryId", entry1.getKey());
                String category2Name = entry1.getValue().get(0).getCategory2Name();
                jsonObject2.put("categoryName", category2Name);
                baseCategoryView2.add(jsonObject2);

                ArrayList<JSONObject> baseCategoryView3 = new ArrayList<>();
                for (BaseCategoryView baseCategoryView : entry1.getValue()) {
                    JSONObject jsonObject3 = new JSONObject();
                    jsonObject3.put("categoryId", baseCategoryView.getCategory3Id());
                    jsonObject3.put("categoryName", baseCategoryView.getCategory3Name());
                    baseCategoryView3.add(jsonObject3);
                }
                jsonObject2.put("categoryChild", baseCategoryView3);
            }
            jsonObject1.put("categoryChild", baseCategoryView2);
            baseCategoryView1.add(jsonObject1);
        }
        return baseCategoryView1;
    }


    /**
     * 获取一级分类下的属性
     */
    @Override
    public List<BaseAttribute> findAttribute(Long category1Id) {
        List<BaseAttribute> baseAttributeList = baseAttributeMapper.findByCategory1Id(category1Id);
        return baseAttributeList;
    }

    @Override
    @GuiGuLogin
    public BaseCategoryView getCategoryView(Long category3Id) {
        BaseCategoryView baseCategoryView = baseCategoryViewMapper.selectById(category3Id);
        return baseCategoryView;
    }

    @Override
    public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {
        //根据一级id返回二级id  将二级集合转换成二级id
        List<BaseCategory2> baseCategory2List = baseCategory2Mapper.selectList(new LambdaQueryWrapper<BaseCategory2>()
                .eq(BaseCategory2::getCategory1Id, category1Id)
                .select((BaseCategory2::getId)));
        //获取二级id
        if (CollUtil.isNotEmpty(baseCategory2List)) {
            List<Long> cateId2 = baseCategory2List.stream()
                    .map(BaseCategory2::getId)
                    .collect(Collectors.toList());

            //根据二级id返回三级id
            List<BaseCategory3> baseCategory3s = baseCategory3Mapper.selectList(new LambdaQueryWrapper<BaseCategory3>()
                    .in(BaseCategory3::getCategory2Id, cateId2)
                    .eq(BaseCategory3::getIsTop, 1)
                    .orderByDesc(BaseCategory3::getOrderNum)
                    .last("limit 7"));
            return baseCategory3s;
        }
        return null;
    }


    /**
     * 根据一级id获取全部的分类的属性
     *
     * @param category1Id
     * @return
     */
    @Override
    public JSONObject getBaseCategoryListById(Long category1Id) {
        //根据一级id获取二级分类的id和value
        JSONObject jsonObject1 = new JSONObject();
        List<BaseCategoryView> categoryList1 = baseCategoryViewMapper.selectList(new LambdaQueryWrapper<BaseCategoryView>()
                .eq(BaseCategoryView::getCategory1Id, category1Id));
        //获取一级分类的id和name
        BaseCategoryView baseCategoryView = categoryList1.get(0);
        jsonObject1.put("categoryId", baseCategoryView.getCategory1Id());
        jsonObject1.put("categoryName", baseCategoryView.getCategory1Name());

        //根据二级分类的集合获取二级分类的id和name
        ArrayList<JSONObject> category2List = new ArrayList<>();
        //根据二级id进行分组
        Map<Long, List<BaseCategoryView>> category2Map = categoryList1.
                stream().
                collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
        for (Map.Entry<Long, List<BaseCategoryView>> entry2 : category2Map.entrySet()) {
            JSONObject jsonObject2 = new JSONObject();
            jsonObject2.put("categoryId", entry2.getKey());
            jsonObject2.put("categoryName", entry2.getValue().get(0).getCategory2Name());
            category2List.add(jsonObject2);
            //根据二级对象获取三级对象分类
            ArrayList<JSONObject> catefory3List = new ArrayList<>();
            for (BaseCategoryView entry3 : entry2.getValue()) {
                JSONObject jsonObject3 = new JSONObject();
                jsonObject3.put("categoryId", entry3.getCategory3Id());
                jsonObject3.put("categoryName", entry3.getCategory3Name());
                catefory3List.add(jsonObject3);
            }
            jsonObject2.put("categoryChild", catefory3List);
        }
        ;
        //封装二级对象分类
        jsonObject1.put("categoryChild", category2List);

        return jsonObject1;
    }

    @Override
    public List<BaseCategory1> findAllCategory1() {
        List<BaseCategory1> baseCategory1s =
                this.list(new LambdaQueryWrapper<BaseCategory1>().select(BaseCategory1::getId));
        return baseCategory1s;
    }
}
